<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btnLogin">登录</button>
    <script>
      //节流函数，在interval时间内将要执行的fn函数
      function throttle(fn, interval) {
        //上一次函数被调用的时间
        let last = 0;
        return function () {
          //上下文
          let ctx = this;
          //参数
          let args = arguments;

          //当前时间
          let now = new Date();
          if (now - last > interval) {
            fn.apply(ctx, args); //执行函数
            last = now; //重要计时
          }
        };
      }

      document.getElementById("btnLogin").addEventListener(
        "click",
        throttle((event) => {
          console.log("向服务器请求登录");
        }, 2000),
        false
      );
    </script>
  </body>
</html>
